package com.lee.starter.config;

import com.lee.starter.response.enhancer.IResultHandler;
import com.lee.starter.response.enhancer.ResultEnhancer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author lee
 * @since 2019-11-22
 */
@SpringBootConfiguration
@ComponentScan("com.lee.starter")
public class ResultAutoConfiguration {

    private IResultHandler finalResultHandler;

    public ResultAutoConfiguration(ObjectProvider<IResultHandler[]> resultHandlers) {

        IResultHandler[] available = resultHandlers.getIfAvailable();

        if (null != available && available.length > 0) {
            finalResultHandler = available[available.length - 1];
        }
    }

    @Bean
    public ResultEnhancer resultEnhancer() {

        ResultEnhancer resultEnhancer = new ResultEnhancer();
        if (null != finalResultHandler) {
            resultEnhancer.setResultHandler(finalResultHandler);
        }
        return resultEnhancer;
    }
}
